package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/consumer"
	"github.com/apache/rocketmq-client-go/v2/primitive"
)

func main() {
	c, _ := rocketmq.NewPushConsumer(
		consumer.WithNameServer([]string{"120.24.67.46:9876"}),
		consumer.WithGroupName("testGroup"),
	)
	/*
		以下方法也可以使用, 未来会被废弃
		c, _ := rocketmq.NewPushConsumer(
			consumer.WithGroupName("testGroup"),
			consumer.WithNsResolver(primitive.NewPassthroughResolver([]string{"120.24.67.46:9876"})),
		)
	*/
	err := c.Subscribe("test", consumer.MessageSelector{}, func(ctx context.Context,
		msgs ...*primitive.MessageExt) (consumer.ConsumeResult, error) {
		for i := range msgs {
			fmt.Printf("收到消息咯....: %v \n", msgs[i])
		}

		// 如果业务逻辑有错误, 那么可以return consumer.ConsumeRetryLater, nil
		return consumer.ConsumeSuccess, nil
	})

	if err != nil {
		fmt.Println(err.Error())
	}

	// Note: start after subscribe
	err = c.Start()
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(-1)
	}
	time.Sleep(time.Hour)
	err = c.Shutdown()
	if err != nil {
		fmt.Printf("shutdown Consumer error: %s", err.Error())
	}
}
